#!/bin/sh
#
# cleanupTNSNames.sh: shell script to cleanup an entry in tnsnames.ora 
#
# usage: cleanupTNSNames.sh <New database sid name> <Oracle Home path> 

nsid=$1
oracleHome=$2
lnn=$3
tnsadmindir=$4

if [ "$notnsupdate" = "true" ]; then
   echo "Do not update tnsNames.ora has been set, no-op"
   exit 0
fi

if [ -z "$nsid" ]; then
   exit 256;
fi

if [ -z "$oracleHome" ]; then
   exit 256
fi  

if [ -z "$lnn" ]; then
   lnn=6
fi

tnsnameora="$oracleHome/network/admin/tnsnames.ora"
tnsnameoratmp="$oracleHome/network/admin/tnsnames.ora.tmp"
if [ ! -z "$tnsadmindir" ]; then
   tnsnameora="$tnsadmindir/tnsnames.ora"
   tnsnameoratmp="$tnsadmindir/tnsnames.ora.tmp"
fi

if [ ! -f $tnsnameora ]; then
   echo "$tnsnameora does not exists"
   exit 0
fi 

linenum=`grep -wni "$nsid" "$tnsnameora" | head -1 | cut -d":" -f1`
linenumend=$(( $linenum + $lnn ))
echo "**************** inside cleanup: linenum: $linenum ****************"
echo "**************** inside cleanup: linenumend: $linenumend ****************"

if [ "X$linenum" = "X" ]
    then
        # echo "line is null"
            exit
fi

sed "${linenum},${linenumend}d" "$tnsnameora" > "$tnsnameoratmp" && cat "$tnsnameoratmp" > "$tnsnameora"
rm "$tnsnameoratmp"

exit 0

